/***************
**
*
*公共JS
*
*/

var appSecret = 'b4fd9dc55b17a580f6bad51f277438a2';
var appkey = 'yqb_web';

var layer_offset = '5px';
var layer_height = '97%';

$(function(){

	$(".modal-header .close").text('');//删除关闭
	src_resize();//改变页面内容大小尺寸

	    //点击顶部 tab 关闭按钮
    $('.menutab').on('click','.menutab_title_close',function(){

        var menutab_length = $(".menutab_title").length;
        var menutab_index = $(".menutab_title").index($(this).parent())+1;
        if(menutab_length<=1){
            window.location.href = "index-index";
            return false;
        }

        $(this).parent().remove();
        var idkeynum = $(this).parent().attr('idkeynum');
        $('.cj'+idkeynum).remove();

        var mbtab = 0;//目标tab
        if(menutab_length>menutab_index){

        }else{
            mbtab = menutab_index-2;
        }

        $('.menutab_title').removeClass('active');
        $('.menutab_title').eq(mbtab).addClass('active');
        $('.cjall').hide();
        $('.cjall').eq(mbtab).show();
        return false;
    });

    //点击顶部 tab 某一项
    $('.menutab').on('click','.menutab_title',function(){
        $('.menutab_title').removeClass('active');
        $(this).addClass('active');
        var idkeynum = $(this).attr('idkeynum');
        $('.cjall').hide();
        $('.cj'+idkeynum).show();

        //导航菜单 增加选中效果
        $('.level1').removeClass('nav_click');
        $('.level2').removeClass('nav_click');
        $('#menulist'+idkeynum).addClass('nav_click');
    });

	//切换子菜单	一级导航
	$('.level1').on('click',function(){
		//导航菜单 增加选中效果
		$('.level1').removeClass('nav_click');
		$(this).addClass('nav_click');
		$('.level2').removeClass('nav_click');

		var url = $(this).attr('link');   //路径

		layer_load();//加载层
		var idkeynum = $(this).attr('idkeynum');

		if ($(".menutab_title"+idkeynum).length > 0 ) {
			//已经存在当前菜单对应的tab
			layer.closeAll('loading'); //关闭加载层
			$('.menutab_title').removeClass('active');
			$(".menutab_title"+idkeynum).addClass('active');

			$('.menutab_title').removeClass('top_menue_active');
			$(".menutab_title"+idkeynum).addClass('top_menue_active');

			$('.cjall').hide();
			$('.cj'+idkeynum).show();
		}else{
			//不存在当前菜单对应的tab
			$('.menutab_title').removeClass('active');
			var menutab_item = '<a class="btn menutab_title menutab_title'+idkeynum+' active" type="button" idkeynum="'+idkeynum+'">'+$(this).text()+' <i class="icon icon-times menutab_title_close"></i></a>';
			$('.menutab').append(menutab_item);

			$('.menutab_title').removeClass('top_menue_active');
			$(".menutab_title"+idkeynum).addClass('top_menue_active');

			if(url){
				menue_chage_html(url,idkeynum);
			}
		}
	});

 	//切换子菜单	二级导航
  	$('.level2').on('click',function(){
  		layer_load();//加载层
  		//导航菜单 增加选中效果
  		$('.level2').removeClass('nav_click');
	  	$(this).addClass('nav_click');
			$('.level1').removeClass('nav_click');

	   	var url = $(this).attr('link');   //路径
		var idkeynum = $(this).attr('idkeynum');

		if ($(".menutab_title"+idkeynum).length > 0 ) {
			//已经存在当前菜单对应的tab
			layer.closeAll('loading'); //关闭加载层
			$('.menutab_title').removeClass('active');
			$(".menutab_title"+idkeynum).addClass('active');

			$('.menutab_title').removeClass('top_menue_active');
			$(".menutab_title"+idkeynum).addClass('top_menue_active');
			list_table();

			$('.cjall').hide();
			$('.cj'+idkeynum).show();
		}else{
			//不存在当前菜单对应的tab
			$('.menutab_title').removeClass('active');
			var menutab_item = '<a class="btn menutab_title menutab_title'+idkeynum+' active" type="button" idkeynum="'+idkeynum+'">'+$(this).text()+' <i class="icon icon-times menutab_title_close"></i></a>';
			$('.menutab').append(menutab_item);

			$('.menutab_title').removeClass('top_menue_active');
			$(".menutab_title"+idkeynum).addClass('top_menue_active');

			if(url){
				menue_chage_html(url,idkeynum);
			}
		}
   	});

	//当点击 带有二级导航 的节点时，去除之前 选中节点 的 选中状态
	$('#menulist2,#menulist3').click(function(){
	  	$('.main_left_menu1').find('.nav_click').removeClass('nav_click');
	});

	//导航菜单 增加选中效果
	var menu_list = '#'+$('#menu_list').val();
	$(menu_list).addClass('nav_click');


	//缩放左侧导航菜单
    $('.left_menu_onoff').click(function(){
    	var onoff = $(this).attr('onoff');
    	if(onoff && 'k' == onoff){
			//emarket
			//$(".infocollect").animate({margin:'0 20px 0 75px'},"slow");

            $(".icon-chevron-right").hide(500);
    		$(".left_menu").animate({width:"55px"},"slow");
	    	$('.left_menu span').fadeOut();

	    	$('.weblogo').removeClass('weblogo_bg').addClass('weblogo_bg1').html('');
	    	$(this).attr('onoff','g');
	    	twoul_onoff();
	    	$('#menulist2').next().attr('btn_dis','y');
	    	var scrwidth=$(document.body).width()-56;//72;
	    	$(".main_right_content1").animate({width:scrwidth+'px'},"slow");
	    	$('#MyTable_tableLayout').animate({marginLeft:60+'px'},"slow");
    	}else{
			//$(".infocollect").animate({margin:'0 20px 0 170px'},"slow");

    		$(".icon-chevron-right").show(300);
    		var twoul = $('#menulist1').next();
			if('on' == twoul.attr('dis')){
				twoul.css('display','block');
			}

	    	var twoul = $('#menulist2').next();
			if('on' == twoul.attr('dis')){
				twoul.css('display','block');
			}

			var twoul = $('#menulist3').next();
			if('on' == twoul.attr('dis')){
				twoul.css('display','block');
			}

    		$(this).attr('onoff','k');
    		//$(".left_menu").animate({width:"148px"},"slow");
    		$(".left_menu").animate({width:"180px"},"slow");

			$('.left_menu span').fadeIn(1000);
			$('.weblogo').removeClass('weblogo_bg1').addClass('weblogo_bg').html('专卖大数据综合管理平台');

			var scrwidth=$(document.body).width()-180;//148;

            $(".main_right_content1").animate({width:scrwidth+'px'},"slow");
            $('#MyTable_tableLayout').animate({marginLeft:0+'px'},"slow");
    	}
    	if($('.content_left_tree').css('display')!='none'){
    		devcontent_width = scrwidth - 200;
    	}else{
    		devcontent_width = scrwidth;
    	}
    	//alert($('.content_left_tree').css('display') + ' || scrwidth = ' +scrwidth+ ' || devcontent_width = '+devcontent_width);
    	$('.devcontent').css('width',devcontent_width+'px');//基本页面的宽度
	});

	//当左侧菜单收起时，显示对应的标题
	$('.menu ul li').mouseover(function(){
		if($(".left_menu").css('width')=='55px'){
			layer.tips($(this).find('span').html(), $(this).find('i'), {tips: [2, '#333333']});
		}
	});

});
	//请求获取数据
	function http_to_request(url,parameter){
		var version = $('input[name="version"]').val();
		var imei = $('input[name="imei"]').val();
		var session = $('input[name="session"]').val();
		parameter += '&version='+version+'&imei='+imei+'&session='+session;

		http_data = url_to_json(parameter);			//参数转json
		http_data = encodeURIComponent(http_data);	//转码

		var http_time = (new Date()).valueOf();		//时间戳
		md5_data = $.md5(http_data);		//参数加密

		var checksum = hex_sha1(appSecret+md5_data+http_time);		//获取加密串
		formdata = 'http_data='+http_data+'&checksum='+checksum+'&http_time='+http_time+'&AppKey='+appkey;	//拼接请求参数

		var result = '';
		layer_load();//加载层

		$.ajax({
			type : "post",
			url : url,
			data : formdata,
			dataType : "html",
			async: false, //同步传输，并添加返回值，返回值应为已定义的全局变量 如a
			success : function(data) {
				
				var json_data = isJsonFormat(data);
				if(json_data){
					json_data = $.parseJSON(data);
					
					if(json_data.code != 200){
						if(json_data.code == 'T200'){
							
							result = json_data.data; 
							layer.msg(json_data.message, {icon: 1});
							layer.closeAll('loading'); //关闭加载层
						}else{
							layer.msg(json_data.message, {icon: 2});
							layer.closeAll('loading'); //关闭加载层
							throw "";
						}

					}

				}else{
					result = data;

				}
			}
		});

		return result;
	}

	//返回加密后的请求地址
	function http_to_request_url(url,parameter){
		var version = $('input[name="version"]').val();
		var imei = $('input[name="imei"]').val();
		var session = $('input[name="session"]').val();
		parameter += '&version='+version+'&imei='+imei+'&session='+session;

		http_data = url_to_json(parameter);			//参数转json
		http_data = encodeURIComponent(http_data);	//转码

		var http_time = (new Date()).valueOf();		//时间戳
		md5_data = $.md5(http_data);		//参数加密

		var checksum = hex_sha1(appSecret+md5_data+http_time);		//获取加密串
		formdata = 'http_data='+http_data+'&checksum='+checksum+'&http_time='+http_time+'&AppKey='+appkey;	//拼接请求参数

		return url+'?'+formdata;
	}

	//判断字符串是否为json
	function isJsonFormat( str ) {
		try{
			$.parseJSON(str);
		}catch(e){
			return false;
		}
		return true;
	}


	//参数转json
	function url_to_json(url){
		var data = url.split('&');
		var json = '{';
		$.each(data,function(k,v){
			var data_n = v.split('=');
			json += '"'+data_n[0]+'":"'+data_n[1]+'",';
		})

		json =  json.substring(0,json.length-1);
		json += '}';
		console.log(json);
		return json;
	}

	$(window).resize(function() {
		src_resize();//改变页面内容大小尺寸
	});

	//菜单点击
	function menue_chage_html(url,idkeynum){
		var version = $('input[name="version"]').val();
		var imei = $('input[name="imei"]').val();
		var session = $('input[name="session"]').val();
		parameter = 'version='+version+'&imei='+imei+'&session='+session;

		http_data = url_to_json(parameter);			//参数转json
		http_data = encodeURIComponent(http_data);

		var http_time = (new Date()).valueOf();		//时间戳

		md5_data = $.md5(http_data);		//数据MD5加密加密
		var checksum = hex_sha1(appSecret+md5_data+http_time);		//加密串
		formdata = 'http_data='+http_data+'&checksum='+checksum+'&http_time='+http_time+'&AppKey='+appkey;		//请求参数

		$.get(url,formdata,function(data){
			layer.closeAll('loading'); //关闭加载层
			data = '<div class="cjall cj'+idkeynum+'" idkeynum="'+idkeynum+'">'+data+'</div>';

			//$('#module_list').remove();
			$('.cjall').hide();
			$('.cj').append(data);

			$('.cj'+idkeynum).show();
		},'html');
	}

	//基础设置 中 选项卡 切换
	function tablink(link){
		//导航菜单 增加选中效果
		$('.tablink').removeClass('menu_click');
		$(this).addClass('menu_click');

	   if(link!='undefined'){
		 $.ajax({
			  type:'post',
			  url:link,
			  dataType:'html',
			  success:function(data){
				  $('.tablediv1').html(data);
			  }
		  });
	   }
	}

	//改变页面内容大小尺寸
	function src_resize(){
		var height = $(window).height()-90;
		$('.infocollect').css('height',height);
		//是否是关闭或者打开左侧导航
		var onoff = $('.left_menu_onoff').attr('onoff');
		/*if(onoff == 'g'){
			$(".infocollect").css('margin','0 20px 0 75px');
		}*/

		$('.main_content').css('height',height);

 		var left_menu_width = $('.left_menu').css('width');

        var ss = $(".filter-box").height();
		if(left_menu_width){
			var menu_width = left_menu_width.replace('px','');
		}
 		
 		var scrwidth=$(document.body).width()-menu_width;

 		$('.main_right_content1').css('width',scrwidth+'px');

		var scrHeight = $(window).height()-60;
		var scrHeightRight = scrHeight - 3;

        var scrHeightRightcon = scrHeight;// - 83- ss;

        $('.main_left_menu1').css('height',scrHeight+'px');
        var tree_height = scrHeightRightcon - 2;//树形结构高度
		$('.content_left_tree').css('height',tree_height+'px');
		var content_left_tree_height = $('.content_left_tree').height();
		if($('.content_left_tree').height()>(tree_height-35)){
			$('.devcontent_right').css('min-height',content_left_tree_height+'px');
		}else{
			$('.devcontent_right').css('min-height',tree_height+'px');
		}

		var devcontent_width = scrwidth-210;//245;
		$('.devcontent').css('width',devcontent_width+'px');//基本页面的宽度
		$('.devcontent').css('height',scrHeightRightcon+'px');//基本页面的高度
		var Nheight = scrHeightRightcon-20;
        $('.devcontentl').css('height',Nheight+'px');//tab页面的高度（客户信息首页）
        var Nheights = scrHeightRightcon+20;
        $('.devcontents').css('height',Nheights+'px');//添加页面的高度（新增日计划）
        var N_tree_height = Nheights-2;
        $('.content_left_tree_s').css('min-height',N_tree_height+'px');
    	$('.devonline_content_right').css('min-height',scrHeightRight+'px');
    	$('.devonline_content_right1').css('height',scrHeightRight+'px');

        var offsethg = scrHeight-$('.teaButs').height()-569;   /*教师中心左侧菜单底部的按钮位置*/

    	$('.teaButs').css('margin-top',offsethg+'px');  /*教师中心左侧菜单底部的按钮位置*/

        // 设置超级查询页面的宽度
        //$(".query_left_div").css("width", $(document.body).width()-620);
        //$(".query_table_data").css("width", $(document.body).width()-620);
        //超级查询页面框架右边DIV高度
        $(".content_left_div").css("height", $(window).height()-110);
        $(".query_super_more").css("height", $(window).height()-110);

	}

	function twoul_onoff(){
		var twoul = $('#menulist1').next();
		if('block' == twoul.css('display')){
			twoul.attr('dis','on');
			twoul.css('display','none');
		}else{
			twoul.attr('dis','off');
		}
		var twoul = $('#menulist2').next();
		if('block' == twoul.css('display')){
			twoul.attr('dis','on');
			twoul.css('display','none');
		}else{
			twoul.attr('dis','off');
		}

		var twoul = $('#menulist3').next();
		if('block' == twoul.css('display')){
			twoul.attr('dis','on');
			twoul.css('display','none');
		}else{
			twoul.attr('dis','off');
		}
	}

	//加载层
	function layer_load(){
		layer.load(2, {content:'加载中。。。',shade: [0.6, '#393D49'],success: function(layero){
			layero.find('.layui-layer-content').css('width', '300px').css('color', '#999').css('font-size', '26px').css('padding-left', '40px');//.css('background', 'none')
		}});
	}

	//zui单选返回选中状态
	function status_selected(class_name,value){
		//营业状态选中
		$(class_name+" option[value='"+value+"']").attr("selected",true);
		$(class_name).on('chosen:ready', function(e, params) {
		  $(class_name).val(value)//设置值
		});
		$(class_name).trigger('chosen:updated');//更新选项
	}
